import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { FormBuilder, Validators, FormGroup } from '@angular/forms'
import { QlMessageService } from 'qloud-angular'
import { plainToClass } from 'class-transformer'
import { DictService } from '@app/shared/utils/dict.service'
import { ApiService } from '../../services/api.service'
import { ModalService } from '@app/shared/utils'
import {CursorService} from "@core/http/cursor.service";
import {ModalConfirmComponent} from "@shared/components/modal-confirm/modal-confirm.component";
import {Store} from "@ngxs/store";

@Component({
  selector: 'app-attention-list',
  templateUrl: './attention-list.component.html',
  styleUrls: ['./attention-list.component.scss'],
  providers: [ApiService, QlMessageService]
})
export class AttentionListComponent implements OnInit {
  productname = '';
  productcode ='';
  productcategory = '';
  recommandtype = '';
  public attentionList: any[] = []
  // public productTypes = [
  //   {value:'1',label:'理财产品'},
  //   {value:'2',label:'基金'},
  //   {value:'3',label:'期货'},
  //   {value:'4',label:'债劵'},
  //   {value:'5',label:'股票'},
  //   {value:'6',label:'贵金属'},
  // ]

  constructor(
    public cursorService: CursorService,
    private store: Store,
    public dictService: DictService,
    private fb: FormBuilder,
    private apiService: ApiService,
    public modal: ModalService,
    private message: QlMessageService) {}

  ngOnInit() {
    console.log('---'+JSON.stringify(this.dictService.getDict('product_type')))
    this.onRefresh()
  }
  onRefresh(){
    this.cursorService.limit = 10;
    let sql='select *'
    if(this.productname!=''){
      sql= this.transform('productname',this.productname ,sql)
    }
    if(this.productcode!=''){
      sql= this.transform('productcode',this.productcode ,sql)
    }
    if(this.productcategory!=''){
      sql= this.transform('productcategory',this.productcategory ,sql)
    }
    if(this.recommandtype!=''){
      sql= this.transform('recommandtype',this.recommandtype ,sql)
    }
    console.log('sql:'+JSON.stringify(sql))
    this.cursorService.statement =sql;
    this.apiService.getAttentionList(this.cursorService).subscribe(data => {
      // console.log('attentionList:'+JSON.stringify(data))
      this.attentionList = data
    })
  }
  transform(key:string,value:string,sql:string){
    if(sql != 'select *'){
      sql += ' and ' + key +" = " + "'*" + value + "*'"
    }
    else{
      sql += " where " + key +" = " + "'*" + value + "*'";
    }
    return sql
  }

  public delFun(productcode) {
    this.modal
      .open({
        title: '确认移除',
        component: ModalConfirmComponent
      })
      .subscribe(() => {
        this.apiService.delFocus(productcode).subscribe(data => {
          this.onRefresh();
          this.message.success('删除成功')
        })
      })
  }
}
